﻿using Microsoft.AspNetCore.Mvc;
using RuYiAdmin.Net.Common.Utility;
using RuYiAdmin.Net.Entity.BusinessEntity.SystemManagement;
using RuYiAdmin.Net.Entity.BusinessEnum;
using RuYiAdmin.Net.Entity.CoreEntity;
using RuYiAdmin.Net.Entity.CoreEnum;
using RuYiAdmin.Net.Service.BusinessService.SystemManagement.Import;
using RuYiAdmin.Net.Service.BusinessService.SystemManagement.ImportDetail;
using RuYiAdmin.Net.WebApi.AppCode.Base;
using RuYiAdmin.Net.WebApi.AppCode.Filter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace RuYiAdmin.Net.WebApi.Controllers.SystemManagement
{
    /// <summary>
    /// 导入配置管理控制器
    /// </summary>
    public class ImportConfigController : BaseController<SysImportConfig>
    {
        /// <summary>
        /// 导入配置接口实例
        /// </summary>
        private readonly IImportConfigService importConfigService;

        /// <summary>
        /// 导入配置明细接口实例
        /// </summary>
        private readonly IImportConfigDetailService importConfigDetailService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="importConfigService"></param>
        /// <param name="importConfigDetailService"></param>
        public ImportConfigController(IImportConfigService importConfigService,
            IImportConfigDetailService importConfigDetailService) : base(importConfigService)
        {
            this.importConfigService = importConfigService;
            this.importConfigDetailService = importConfigDetailService;
        }

        /// <summary>
        /// 查询导入配置列表
        /// </summary>
        /// <param name="queryCondition">查询条件</param>
        /// <returns>ActionResult</returns>
        [HttpPost]
        [Log(OperationType.QueryList)]
        [Permission("import:config:query:list")]
        public IActionResult Post(QueryCondition queryCondition)
        {
            var actionResult = this.importConfigService.SqlQuery(queryCondition, "sqls:sql:query_sysimportconfig");
            return Ok(actionResult);
        }

        /// <summary>
        /// 按编号获取信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>ActionResult</returns>
        [HttpGet("{id}")]
        [Log(OperationType.QueryEntity)]
        [Permission("import:config:query:list")]
        public IActionResult GetById(Guid id)
        {
            var actionResult = this.importConfigService.GetById(id);
            return Ok(actionResult);
        }

        /// <summary>
        /// 按父键获取明细
        /// </summary>
        /// <param name="parentId">父键</param>
        /// <returns>ActionResult</returns>
        [HttpGet("{parentId}")]
        [Log(OperationType.QueryEntity)]
        [Permission("import:config:query:list")]
        public IActionResult GetByParentId(Guid parentId)
        {
            var actionResult = new Entity.CoreEntity.ActionResult();

            var list = (List<SysImportConfigDetail>)this.importConfigDetailService.GetList().Object;

            list = list.Where(t => t.IsDel == 0).OrderByDescending(t=>t.CreateTime).ToList();

            actionResult.HttpStatusCode = HttpStatusCode.OK;
            actionResult.Message = new String("OK");
            actionResult.Object = list.Where(t => t.ParentId.Equals(parentId)).OrderBy(t => t.Order).ToList();

            return Ok(actionResult);
        }

        /// <summary>
        /// 添加导入配置
        /// </summary>
        /// <param name="importConfig">导入配置</param>
        /// <returns>ActionResult</returns>
        [HttpPost]
        [Log(OperationType.AddEntity)]
        [Permission("import:config:add:entity")]
        public IActionResult AddConfig([FromBody] SysImportConfig importConfig)
        {
            var actionResult = this.importConfigService.Add(importConfig);

            return Ok(actionResult);
        }

        /// <summary>
        ///添加导入配置明细
        /// </summary>
        /// <param name="configDetail">配置明细</param>
        /// <returns>ActionResult</returns>
        [HttpPost]
        [Log(OperationType.AddEntity)]
        [Permission("import:config:add:entity")]
        public IActionResult AddConfigDetail([FromBody] SysImportConfigDetail configDetail)
        {
            var actionResult = this.importConfigDetailService.Add(configDetail);

            return Ok(actionResult);
        }

        /// <summary>
        /// 编辑导入配置
        /// </summary>
        /// <param name="importConfig">导入配置</param>
        /// <returns>ActionResult</returns>
        [HttpPut]
        [Log(OperationType.EditEntity)]
        [Permission("import:config:edit:entity")]
        public IActionResult EditConfig([FromBody] SysImportConfig importConfig)
        {
            var actionResult = this.importConfigService.Update(importConfig);

            return Ok(actionResult);
        }

        /// <summary>
        ///编辑导入配置明细
        /// </summary>
        /// <param name="configDetail">配置明细</param>
        /// <returns>ActionResult</returns>
        [HttpPut]
        [Log(OperationType.EditEntity)]
        [Permission("import:config:edit:entity")]
        public IActionResult EditConfigDetail([FromBody] SysImportConfigDetail configDetail)
        {
            var actionResult = this.importConfigDetailService.Update(configDetail);

            return Ok(actionResult);
        }

        /// <summary>
        /// 按编号删除
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>ActionResult</returns>
        [HttpDelete("{id}")]
        [Log(OperationType.DeleteEntity)]
        [Permission("import:config:del:entities")]
        public IActionResult DeleteConfig(Guid id)
        {
            if (this.Check(id))
            {
                return BadRequest("config contains subitems,can not be deleted");
            }

            var actionResult = this.importConfigService.Delete(id);

            return Ok(actionResult);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids">数组串</param>
        /// <returns>ActionResult</returns>
        [HttpDelete("{ids}")]
        [Log(OperationType.DeleteEntity)]
        [Permission("import:config:del:entities")]
        public IActionResult DeleteConfigs(String ids)
        {
            var array = StringUtil.GetGuids(ids);

            foreach (var item in array)
            {
                if (this.Check(item))
                {
                    return BadRequest("config contains subitems,can not be deleted");
                }
            }

            var actionResult = this.importConfigService.DeleteRange(array);

            return Ok(actionResult);
        }

        /// <summary>
        /// 按编号删除配置明细
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>ActionResult</returns>
        [HttpDelete("{id}")]
        [Log(OperationType.DeleteEntity)]
        [Permission("import:config:del:entities")]
        public IActionResult DeleteConfigDetail(Guid id)
        {
            var actionResult = this.importConfigDetailService.Delete(id);

            return Ok(actionResult);
        }

        /// <summary>
        /// 批量删除配置明细
        /// </summary>
        /// <param name="ids">数组串</param>
        /// <returns>ActionResult</returns>
        [HttpDelete("{ids}")]
        [Log(OperationType.DeleteEntity)]
        [Permission("import:config:del:entities")]
        public IActionResult DeleteConfigDetails(String ids)
        {
            var array = StringUtil.GetGuids(ids);

            var actionResult = this.importConfigDetailService.DeleteRange(array);

            return Ok(actionResult);
        }

        /// <summary>
        /// 子表检测
        /// </summary>
        /// <param name="parentId">父键</param>
        /// <returns>真假值</returns>
        private bool Check(Guid parentId)
        {
            var queryCondition = new QueryCondition();

            queryCondition.QueryItems = new List<QueryItem>();

            queryCondition.QueryItems.Add(new QueryItem()
            {
                Field = "ParentId",
                DataType = DataType.Guid,
                QueryMethod = QueryMethod.Equal,
                Value = parentId
            });

            var data = this.importConfigDetailService.GetList(queryCondition);

            if (data.List.Count > 0)
            {
                return true;
            }

            return false;
        }
    }
}
